package Admin;

import java.awt.EventQueue;
import java.io.File;
import java.io.FileOutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JInternalFrame;

import java.awt.BorderLayout;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.example.finalproj.ChatData;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Chat_Server_Log extends JInternalFrame implements Runnable{
		
	boolean flag=true;
	HashMap<String, ServerThread> user;
	HashMap<String, Chat_Internal_Frame> design;
	ServerSocket server;
	
	JDesktopPane main_jdp;
	
	private JPanel panel;
	private JScrollPane scrollPane;
	JTextArea ta_log;
	private JTextField tf_msg;
	private JButton btn_send;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Chat_Server_Log frame = new Chat_Server_Log();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Chat_Server_Log() {
		setMaximizable(true);
		setResizable(true);
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.SOUTH);
		getContentPane().add(getScrollPane(), BorderLayout.CENTER);
		user = new HashMap<String, ServerThread>();
		setBounds(10, 10, 450, 300);
	}
	public Chat_Server_Log(JDesktopPane main_jdp, HashMap<String, Chat_Internal_Frame> design) {
		this();
		this.main_jdp = main_jdp;
		this.design = design;
		try
		{
			String ip = InetAddress.getLocalHost().getHostAddress();
			setTitle("채팅로그 - "+ip);
			
//			ta_log.append("설마");
			new Thread(this).start();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	@Override
	public void run()
	{
		try
		{
			server = new ServerSocket(12007);
//			setTitle("채팅로그 - "+server.getInetAddress().getHostAddress());
			ta_log.append("서버가 시작 되었습니다\n");
			
			while(flag)
			{
				Socket s = server.accept();
				new ServerThread(user, s, ta_log, main_jdp, design);
			}
			server.close();
			server = null;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getTf_msg());
			panel.add(getBtn_send(), BorderLayout.EAST);
		}
		return panel;
	}
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTa_log());
		}
		return scrollPane;
	}
	public JTextArea getTa_log() {
		if (ta_log == null) {
			ta_log = new JTextArea();
			ta_log.setEditable(false);
		}
		return ta_log;
	}
	public JTextField getTf_msg() {
		if (tf_msg == null) {
			tf_msg = new JTextField();
			tf_msg.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent arg0) {
					if(arg0.getKeyCode()==10)
					{
						sendgong();
					}
				}
			});
			tf_msg.setColumns(10);
		}
		return tf_msg;
	}
	public void sendgong()
	{
		Iterator<String> it = user.keySet().iterator();
		ChatData c = new ChatData();
		c.setAction(4);
		c.setMsg(tf_msg.getText());
		while(it.hasNext())
		{
			try
			{
				user.get(it.next()).oos.writeObject(c);
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
		}
		ta_log.append("공지 : "+tf_msg.getText()+"\n");
		Iterator<String> iter = design.keySet().iterator();
		while(iter.hasNext())
		{
			Chat_Internal_Frame cif1 = design.get(iter.next());
			Iterator<String> tal = cif1.talk.keySet().iterator();
			while(tal.hasNext())
			{
				cif1.talk.get(tal.next()).appMsg(c);
			}
		}
		tf_msg.setText("");
	}
	public JButton getBtn_send() {
		if (btn_send == null) {
			btn_send = new JButton("공지 전송");
			btn_send.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
//					String msg = tf_msg.getText();
					sendgong();
				}
			});
		}
		return btn_send;
	}
	public void savelog()
	{
		try
		{
			long time = System.currentTimeMillis();
			SimpleDateFormat dayTime = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
			String str = dayTime.format(new Date(time));
			
			File f = new File("C:\\manage\\chat_log");
			f.mkdirs();
			File ff = new File(f.getPath()+"\\"+str+".txt");
			FileOutputStream fos = new FileOutputStream(ff);
			fos.write(ta_log.getText().getBytes());
			fos.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
